package com.yiyun.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yiyun.blog.dao.mapper.SysUserMapper;
import com.yiyun.blog.dao.pojo.SysUser;
import com.yiyun.blog.service.SysUserService;
import com.yiyun.blog.service.TokenService;
import com.yiyun.blog.vo.ErrorCode;
import com.yiyun.blog.vo.LoginUserVo;
import com.yiyun.blog.vo.Result;
import com.yiyun.blog.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Bao
 * @create 2022-01-09-11:25
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TokenService tokenService;

    @Override
    public UserVo findUserVoById(Long id) {

        SysUser sysUser = sysUserMapper.selectById(id);

        if (sysUser == null){
            sysUser = new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/img/logo.b3a48c0.png");
            sysUser.setNickname("杨无敌");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);
        userVo.setId(String.valueOf(sysUser.getId()));
        return userVo;
    }

    @Override
    public SysUser findUserById(Long id) {

        SysUser sysUser = sysUserMapper.selectById(id);

        if (sysUser == null){
            sysUser = new SysUser();
            sysUser.setNickname("杨无敌");
        }
        return sysUser;
    }

    @Override
    public SysUser findUser(String account, String password) {

        //用来设置SQL语句的查询条件的类
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<SysUser>();
        //构造查询条件
        // SELECT id,account,avatar,nickname FROM ms_sys_user WHERE (account = ? AND password = ?) limit 1
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.eq(SysUser::getPassword,password);
        queryWrapper.select(SysUser::getId,SysUser::getAccount,SysUser::getAvatar,SysUser::getNickname);
        queryWrapper.last("limit 1");

        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public Result findUserByToken(String token) {
        /**
         * 1、token合法性检验
         *      是否为空，解析是否成功 redis是否存在
         * 2、如果校验失败，返回错误
         * 3、如果成功，返回对应的结果 LoginUserVo
         * */
        SysUser sysUser = tokenService.checkToken(token);
        if (sysUser == null){
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(),ErrorCode.TOKEN_ERROR.getMsg());
        }

        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setId(String.valueOf(sysUser.getId()));
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setNickname(sysUser.getNickname());

        return Result.success(loginUserVo);
    }

    //根据账户查找用户
    @Override
    public SysUser findUserByAccount(String account) {

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1");

        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);

        return sysUser;
    }

    //保存用户信息
    @Override
    public void save(SysUser sysUser) {

        //保存用户这个 id会自动生成
        //这个地方 默认生成的id是 分布式id 雪花算法
        //mybatis-plus
        this.sysUserMapper.insert(sysUser);
    }
}
